package com.whnk.pm.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.whnk.common.support.Convert;
import com.whnk.common.utils.StringUtils;
import com.whnk.pm.domain.WorkInfo;
import com.whnk.pm.dto.Duration;
import com.whnk.pm.mapper.WorkInfoMapper;
import com.whnk.pm.service.IPmCommonService;
import com.whnk.pm.service.IWorkInfoService;
import com.whnk.pm.util.DateUtil;

/**
 * 工作经历 服务层实现
 * 
 * @author whwhnk
 * @date 2019-06-13
 */
@Service("WorkInfoServiceImpl")
public class WorkInfoServiceImpl implements IWorkInfoService 
{
	@Autowired
	private WorkInfoMapper workInfoMapper;

	@Autowired
	private IPmCommonService iPmCommonService;

	public List<String> getType(String empId)
	{
		WorkInfo workInfo = new WorkInfo();
		workInfo.setEmpId(empId);
		List<String> stringList = workInfoMapper.selectWorkInfoList(workInfo).stream().map(t -> t.getEmpComp()).collect(Collectors.toList());
		return stringList;
	}

	/**
     * 查询工作经历信息
     * 
     * @param workId 工作经历ID
     * @return 工作经历信息
     */
    @Override
	public WorkInfo selectWorkInfoById(Integer workId)
	{
	    return workInfoMapper.selectWorkInfoById(workId);
	}
	
	/**
     * 查询工作经历列表
     * 
     * @param workInfo 工作经历信息
     * @return 工作经历集合
     */
	@Override
	public List<WorkInfo> selectWorkInfoList(WorkInfo workInfo)
	{
		List<WorkInfo> workInfoList = workInfoMapper.selectWorkInfoList(workInfo);
		workInfoList.sort(Comparator.comparing((k1) -> (
				(k1.getEmpWorkEddt() == null ? new Date(): k1.getEmpWorkEddt())
		)));
		return workInfoList;
	}
	
    /**
     * 新增工作经历
     * 
     * @param workInfo 工作经历信息
     * @return 结果
     */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int insertWorkInfo(WorkInfo workInfo)
	{
		iPmCommonService.eduAndWordCheck(workInfo.getEmpId(), workInfo.getEmpWorkStdt());
		int result = workInfoMapper.insertWorkInfo(workInfo);
		staticWord(workInfo.getEmpId());
	    return result;
	}
	
	/**
     * 修改工作经历
     * 
     * @param workInfo 工作经历信息
     * @return 结果
     */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateWorkInfo(WorkInfo workInfo)
	{
		iPmCommonService.eduAndWordCheck(workInfo.getEmpId(), workInfo.getEmpWorkStdt());
//		int result = workInfoMapper.updateWorkInfo(workInfo);
		int result = workInfoMapper.insertWorkInfo(workInfo);
		staticWord(workInfo.getEmpId());
	    return result;
	}

	/**
     * 删除工作经历对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
    @Transactional(rollbackFor = Exception.class)
	public int deleteWorkInfoByIds(String ids)
	{
		String[] strings = Convert.toStrArray(ids);
		String empId = workInfoMapper.selectWorkInfoById(Integer.parseInt(strings[0])).getEmpId();
		int result = workInfoMapper.deleteWorkInfoByIds(strings);
		staticWord(empId);
		return result;
	}

	/**
	 * 验证时间 不可重复
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String checkDate(String stDt, String endDt, String empid, String operationType, String id) throws ParseException {
		DateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM");
		Date startDate = dateTimeFormatter.parse(stDt);
		if (StringUtils.isBlank(endDt)) {
			endDt = dateTimeFormatter.format(new Date());
		}
		Date endDate = dateTimeFormatter.parse(endDt);
		if (endDate.before(startDate)) {
			throw new RuntimeException();
		}
		if (StringUtils.equalsIgnoreCase("edit", operationType)) {
			workInfoMapper.deleteWorkInfoById(Integer.parseInt(id));
		}
		WorkInfo workInfo = new WorkInfo();
		workInfo.setEmpId(empid);
		List<Duration> list =
				workInfoMapper.selectWorkInfoList(workInfo).stream().map(k ->
						new Duration(k.getEmpWorkStdt(), k.getEmpWorkEddt())
				).collect(Collectors.toList());
		return DateUtil.durationOverlap(startDate, endDate, list, operationType);
	}

	private void staticWord(String empId) {
		WorkInfo workInfo = new WorkInfo();
		workInfo.setEmpId(empId);
		List<WorkInfo> workInfoList = workInfoMapper.selectWorkInfoList(workInfo);
		Map<String, List<WorkInfo>> map = new HashMap<>();
		map.put(empId, workInfoList);
		iPmCommonService.staticWorkYears(map);
	}
}
